package algoTests;

import java.util.HashSet;
import java.util.List;
import java.util.Set;

import org.junit.Test;

import solverStrategies.NakedSingle;
import sudoku.SudokuEinzelfeld;
import sudoku.SudokuSpielfeld;

public class RowCol {

	SudokuSpielfeld sf;
	private int successCounter = 0;

	@Test
	public void test() {
		String path = "src/main/resources/sudoku.txt";
		sf = new SudokuSpielfeld(path);
		NakedSingle ns = new NakedSingle();
		ns.manipulateSudoku(sf);
		sf.printSudoku();
		check();
		sf.printSudoku();
		System.out.println(successCounter);
	}

	private void check() {
		// nummer : row : col
		//int[][] arr = new int[10][3];
		for (int block = 1; block < 10; block++) {
			List<SudokuEinzelfeld> blockFedler = sf.returnblock(block);
			//Actual number = 1,2,....9
			for (int actualNumber = 1; actualNumber < 10; actualNumber++) {
				Set<Integer> rows = new HashSet<Integer>();
				Set<Integer> cols = new HashSet<Integer>();
				for (SudokuEinzelfeld sudokuEinzelfeld : blockFedler) {
					if (sudokuEinzelfeld.getNumber() == 0) {
						Set<Integer> poss = sudokuEinzelfeld
								.getPossibleNumbers();
						for (Integer integer : poss) {
							if (integer == actualNumber) {
								cols.add(sudokuEinzelfeld.getCol());
								rows.add(sudokuEinzelfeld.getRow());
							}
						}
					}
				}
				if (rows.size() == 1) {
					List<SudokuEinzelfeld> row = sf.returnRow((Integer) rows
							.toArray()[0]);
					System.out.println("Deleting Nr. " + actualNumber + " from row Nr. " + (Integer) rows
							.toArray()[0]);
					for (SudokuEinzelfeld sudokuEinzelfeld : row) {
						if(sudokuEinzelfeld.removeNumberFromPossibleNumbers(actualNumber)){
							successCounter++;
						}
					}
				}
				if (cols.size() == 1) {
					List<SudokuEinzelfeld> col = sf.returnCol((Integer) cols
							.toArray()[0]);
					System.out.println("Deleting Nr. " + actualNumber + " from col Nr. " + (Integer) rows
							.toArray()[0]);
					for (SudokuEinzelfeld sudokuEinzelfeld : col) {
						if(sudokuEinzelfeld.getBlockNr()!= block){
							if(sudokuEinzelfeld.removeNumberFromPossibleNumbers(actualNumber)){
								successCounter++;
							}
						}
					}
				}
			}
		}
	}

}
